package com.fl.niu.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fl.niu.common.api.CommonResult;
import com.fl.niu.dto.PermissionNode;
import com.fl.niu.model.AdminPermission;
import com.fl.niu.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台用户权限管理
 * @author admin
 */
@Controller
@Api(tags = "UmsPermissionController", description = "后台用户权限管理")
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @ApiOperation("添加权限")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:create')")
    public CommonResult create(@RequestBody AdminPermission permission) {
        int count = permissionService.create(permission);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改权限")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:update')")
    public CommonResult update(@PathVariable Long id, @RequestBody AdminPermission permission) {
        int count = permissionService.update(id,permission);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改权限是否禁用")
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:update')")
    public CommonResult updateHidden(@PathVariable Long id, @RequestBody AdminPermission permission) {
        int count = permissionService.update(id,permission);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("根据id批量删除权限")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:delete')")
    public CommonResult delete(@PathVariable Long id) {
        boolean count = permissionService.removeById(id);
        if(count){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取权限详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:read')")
    public CommonResult<AdminPermission> get(@PathVariable Long id) {
        AdminPermission adminPermission = permissionService.getById(id);
        return CommonResult.success(adminPermission);
    }

    @ApiOperation("以层级结构返回所有权限")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:read')")
    public CommonResult<List<PermissionNode>> treeList() {
        List<PermissionNode> permissionNodeList = permissionService.treeList();
        return CommonResult.success(permissionNodeList);
    }

    @ApiOperation("获取所有权限列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:read')")
    public CommonResult<List<AdminPermission>> list() {
        List<AdminPermission> permissionList = permissionService.list();
        return CommonResult.success(permissionList);
    }

    @ApiOperation("获取除按钮的权限列表")
    @RequestMapping(value = "/notButtonList", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('ums:permission:read')")
    public CommonResult<List<AdminPermission>> notButtonList() {
        List<AdminPermission> permissionList = permissionService.list(new QueryWrapper<AdminPermission>().ne("type",2).eq("status",1).orderByAsc("sort"));
        return CommonResult.success(permissionList);
    }
}
